package com.vm.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

import com.vm.activity.TopActivity;
import com.vm.activity.PresentActivity;
import com.vm.activity.SearchActivity;
import com.vm.adapter.FragAdapter;
import com.vm.myview.NoScrollViewPager;
import com.wwt.app.R;

public class IndexFragment extends BaseFragment {

	private View view;
	RadioButton rbtn_found;
	Button btn_present;
	RadioButton rbtn_new;
	Button btn_top;

	private NoScrollViewPager vp;

	/** 标志位，标志已经初始化完成 */
	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("CategoryFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.index, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		isPrepared = true;
		lazyLoad();

	}

	/**
	 * 初始化控件
	 */
	public void init() {
		rbtn_found = (RadioButton) view.findViewById(R.id.rbtn_found);
		btn_present = (Button) view.findViewById(R.id.btn_present);
		rbtn_new = (RadioButton) view.findViewById(R.id.rbtn_new);
		btn_top = (Button) view.findViewById(R.id.btn_top);

		rbtn_found.setOnClickListener(btn_listener);
		btn_present.setOnClickListener(btn_listener);
		rbtn_new.setOnClickListener(btn_listener);
		btn_top.setOnClickListener(btn_listener);

	}

	OnClickListener btn_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent;
			switch (v.getId()) {
			case R.id.rbtn_found:

				break;
			case R.id.btn_present:
				intent = new Intent(getActivity(),PresentActivity.class);
				startActivityForResult(intent, Activity.RESULT_FIRST_USER);

				break;
			case R.id.rbtn_new:

				break;
			case R.id.btn_top:
				intent = new Intent(getActivity(),TopActivity.class);
				startActivityForResult(intent, Activity.RESULT_FIRST_USER);
				break;

			default:
				break;
			}
		}
	};

	public OnClickListener searchClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(getActivity(), SearchActivity.class);
			startActivity(intent);
		}
	};

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
//				UIHelper.showDialogForLoading(getActivity(), "正在加载...", true);
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(300);
					iniChilds();
					// 在这里添加调用接口获取数据的代码
					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功
					init();
					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
//				UIHelper.hideDialogForLoading();
			}
		}.execute();
	}

	private void iniChilds() {
		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(new FoundFragment());

		// fragments.add(new CategoryGameFragment());
		vp = (NoScrollViewPager) view.findViewById(R.id.viewpager);
		FragAdapter adapter = new FragAdapter(getChildFragmentManager(),
				fragments);
		vp.setAdapter(adapter);

		// 设置最大缓存数
		// 默认缓存2个页面,当出现第三个页面会删掉之前缓存中的一个.
		vp.setOffscreenPageLimit(1);
		vp.setCurrentItem(0);
	}
}
